﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using DAL;

namespace PeisongWeb.admin
{
    public partial class companyPriceStructure : adminPage
    {
        protected int companyId
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["companyId"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["companyId"] = value; }
        }

        

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                


                if (hasPermission("FC", 1))
                {
                    if (Request.QueryString["company"] != null)
                    {
                        if (Library.Number.IsIntPositive(Request.QueryString["company"]))
                        {
                            companyId = Convert.ToInt32(Request.QueryString["company"]);
                            lbl_CompanyName.Text = logic.company.getById(companyId).Company;
                        }
                    }
                    rptBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }

        }

        private void rptBind()
        {

            DataTable dt = logic.category.listWithNewParentByCompany(companyId);
            rpt_list.DataSource = dt;
            rpt_list.DataBind();
        }

        protected void bt_save_Click(object sender, EventArgs e)
        {
            //string[] splitstr = { "," };
            //string[] strs = hd_pricePercent.Value.Split(splitstr, StringSplitOptions.RemoveEmptyEntries);

            string categoryAutoPercentMap=hd_pricePercent.Value;

            logic.priceStructure.Init(logic.sysAdmin.AdminID,companyId,categoryAutoPercentMap);
            ////删除原来价格体系
            //logic.priceStructure.deleteByCompany(companyId);

            //for (int i = 0; i < strs.Length; i=i+2)
            //{
            //    int categoryId = Convert.ToInt32(strs[i + 1]);
            //    DataTable productDt = logic.product.getByCategory(categoryId);

            //    foreach (DataRow dr in productDt.Rows)
            //    {
            //        PriceStructureEntity ps = new PriceStructureEntity();
            //        ps.CategoryId = categoryId;
            //        ps.ProductId = Convert.ToInt32(dr["productId"]);
            //        ps.CompanyId = companyId;
            //        ps.AutoPercent = Convert.ToDecimal(strs[i]);

            //        logic.priceStructure.insert(ps);  //加入价格体系

            //        logic.buyerPriceHistory.insertByUpdateBuyerPriceStuctrue(ps.ProductId,companyId); //增加采购商历史价格
            //    }
            //}

            
            Library.Script.ClientMsgUrl("企业价格体系初始化成功!", "companyList.aspx");

        }

        protected void lbn_Import_Click(object sender, EventArgs e)
        {
            DataTable dt = logic.product.listWithPriceByBuyer(companyId);
            //调用xlsToExcel方法生成Excel报表
        }
    }
}
